/*
 * Created by SharpDevelop.
 * User: GZamor1
 * Date: 7/24/2007
 * Time: 9:34 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MediaPortal.GUI.Library;

namespace OnlineVideos
{
	/// <summary>
	/// Description of Configuration.
	/// </summary>
	public partial class Configuration : Form
	{
		//private String msSelectedSiteID;
		private String msSelectedCategoryName;
        private int miSelectedCategoryIndex = -1;

		private List<GUIOnlineVideos.SiteSettings> moSiteList = new List<GUIOnlineVideos.SiteSettings>();
        
		public Configuration()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		public void Configuration_Load(object sender, EventArgs e){
			OnlineVideoSettings settings = OnlineVideoSettings.getInstance();
			txtThumbLoc.Text = settings.msThumbLocation;
            txtDownloadDir.Text = settings.msDownloadDir;
			String lsFilterList = "";
			String [] lsFilterArray = settings.msFilterArray;

			if(lsFilterArray!=null){
				foreach (String lsFilter in lsFilterArray){
					lsFilterList+=lsFilter+",";
				}
				txtFilters.Text = lsFilterList;
			}            
			foreach(GUIOnlineVideos.SiteSettings site in settings.moSiteList.Values){
				siteList.Items.Add(site.name);
				moSiteList.Add(site);
			}
			foreach(ITMSClient.TrailerSize size in Enum.GetValues(typeof(ITMSClient.TrailerSize))){
				
				cmbTrailerSize.Items.Add(size);
				if(size==OnlineVideoSettings.getInstance().AppleTrailerSize){
					cmbTrailerSize.SelectedIndex = cmbTrailerSize.Items.Count-1;
				}
			}
            this.mplayerUse.Checked = settings.mbForceMPlayer;
		}
		
		void SiteListSelectedIndexChanged(object sender, EventArgs e)
		{
			if(siteList.SelectedIndex>-1){
				OnlineVideoSettings settings = OnlineVideoSettings.getInstance();
				GUIOnlineVideos.SiteSettings site = moSiteList[siteList.SelectedIndex];
				txtSiteName.Text = site.name;
				TxtSiteId.Text = site.id;
				txtUserId.Text = site.username;
				txtPassword.Text = site.password;
				chkEnabled.Checked = site.enabled;
				chkAgeConfirm.Checked = site.confirmAge;
                SiteUtilBase  siteUtil= SiteUtilFactory.getSiteUtil(site.id);
                if (siteUtil.hasConfirmAge())
                {
                    chkAgeConfirm.Enabled = true;
                }
                else
                {
                    chkAgeConfirm.Enabled = false;
                }
                if(siteUtil.hasLoginSupport()){
                    txtUserId.Enabled = true;
                    txtPassword.Enabled = true;
                }else{
                    txtUserId.Enabled = false;
                    txtPassword.Enabled = false;
                }
                txtSiteName.Enabled = true;
                btnSiteSave.Enabled = true;
                btnDeleteRss.Enabled = true;
                btnRssSave.Enabled = true;
                btnAdd.Enabled = true;
                
				CategoryList.Items.Clear();
				foreach(String name in site.RssList.Keys){
					CategoryList.Items.Add(name);
				}
				
			}
			CategoryList.SelectedIndex = -1;
			txtRssUrl.Text = "";
			txtRssName.Text = "";
            txtRssUrl.Enabled = false;
            txtRssName.Enabled = false;
            btnRssSave.Enabled = false;
		}
		
		void CategoryListSelectedIndexChanged(object sender, EventArgs e)
		{
            if (CategoryList.SelectedIndex !=miSelectedCategoryIndex && CategoryList.SelectedIndex > -1)
            {

                OnlineVideoSettings settings = OnlineVideoSettings.getInstance();
                GUIOnlineVideos.SiteSettings site = moSiteList[siteList.SelectedIndex];
                msSelectedCategoryName = CategoryList.SelectedItem.ToString();
                miSelectedCategoryIndex = CategoryList.SelectedIndex;
                Log.Info("Category change site:{0} with selected category of {1}", site.name, msSelectedCategoryName);
                GUIOnlineVideos.RssLink link = null;
                link = site.RssList[msSelectedCategoryName];                
                txtRssUrl.Text = link.url;
                txtRssName.Text = link.name;
                txtRssUrl.Enabled = true;
                txtRssName.Enabled = true;
                btnRssSave.Enabled = true;

            }
            if (CategoryList.SelectedIndex == -1)
            {
                txtRssUrl.Enabled = false;
                txtRssName.Enabled = false;
                btnRssSave.Enabled = false;
                
            }
		}
		
		void BtnSiteSaveClick(object sender, EventArgs e)
		{
			OnlineVideoSettings settings = OnlineVideoSettings.getInstance();
			GUIOnlineVideos.SiteSettings site = moSiteList[siteList.SelectedIndex];
			//site.id = TxtSiteId;
			site.name = txtSiteName.Text;
			site.username = txtUserId.Text;
			site.password = txtPassword.Text;
			site.confirmAge = chkAgeConfirm.Checked;
			site.enabled = chkEnabled.Checked;
			siteList.Items[siteList.SelectedIndex] = site.name;
//			txtSiteName.Text = "";
//			txtUserId.Text = "";
//			chkAgeConfirm.Checked = false;
//			TxtSiteId.Text = "";
//			txtUserId.Text = "";
//			txtPassword.Text = "";
//			siteList.SelectedIndex = -1;
			//msSelectedSiteName = String.Empty;
			//siteList.SelectedIndex = -1;
			//settings.moSiteList.Remove(msSelectedSiteName);
			//msSelectedSiteName = txtSiteName.Text;
			//settings.moSiteList.Add(msSelectedSiteName,site);
		}
		
		void BtnRssSaveClick(object sender, EventArgs e)
		{
            if (CategoryList.SelectedIndex > -1)
            {
                OnlineVideoSettings settings = OnlineVideoSettings.getInstance();
                GUIOnlineVideos.SiteSettings site = moSiteList[siteList.SelectedIndex];
                GUIOnlineVideos.RssLink link = null;
                link = site.RssList[msSelectedCategoryName];
                site.RssList.Remove(msSelectedCategoryName);
                link.name = txtRssName.Text;
                link.url = txtRssUrl.Text;
                CategoryList.Items[CategoryList.SelectedIndex] = txtRssName.Text;
                site.RssList.Add(link.name, link);
                //site.RssList.Remove(msSelectedCategoryName);
                //msSelectedCategoryName = txtRssName.Text;
                //site.RssList.Add(msSelectedCategoryName,link);
                CategoryList.SelectedIndex = -1;
            }
		}
		
		void BtnAddClick(object sender, EventArgs e)
		{            
			GUIOnlineVideos.RssLink link = new GUIOnlineVideos.RssLink();
			link.name = "new";
			link.url = "http://";
			link.isDynamic = false;
			GUIOnlineVideos.SiteSettings site = moSiteList[siteList.SelectedIndex];
			site.RssList.Add(link.name,link);
			CategoryList.Items.Add(link.name);
			CategoryList.SelectedIndex = CategoryList.Items.Count-1;
			txtRssName.Focus();
            msSelectedCategoryName = "new";
		}
		
		
		void BtnDeleteRssClick(object sender, EventArgs e)
		{
			if(CategoryList.SelectedIndex>-1){
				OnlineVideoSettings settings = OnlineVideoSettings.getInstance();
				GUIOnlineVideos.SiteSettings site = moSiteList[siteList.SelectedIndex];
				msSelectedCategoryName = CategoryList.SelectedItem.ToString();
				site.RssList.Remove(msSelectedCategoryName);
				CategoryList.Items.RemoveAt(CategoryList.SelectedIndex);
				txtRssName.Text = "";
				txtRssUrl.Text = "";
                miSelectedCategoryIndex = -1;
			}
			
		}
		
		void ConfigurationFormClosing(object sender, FormClosingEventArgs e)
		{
			OnlineVideoSettings settings = OnlineVideoSettings.getInstance();
			String lsFilter = txtFilters.Text;			
			String [] lsFilterArray = lsFilter.Split(new char[] { ',' });
			settings.msFilterArray = lsFilterArray;
			settings.msThumbLocation = txtThumbLoc.Text;
            Log.Info("OnlineVideo Configuration - download Dir:" + txtDownloadDir.Text);
			settings.msDownloadDir = txtDownloadDir.Text;
			settings.AppleTrailerSize = (ITMSClient.TrailerSize) cmbTrailerSize.SelectedItem;
            settings.mbForceMPlayer = mplayerUse.Checked;
            Log.Info("force mplayer={0}",settings.mbForceMPlayer);
			settings.moSiteList.Clear();
			foreach(GUIOnlineVideos.SiteSettings site in moSiteList){
				settings.moSiteList.Add(site.id,site);
			}
			settings.saveSettings();
			
			
		}

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mplayerUse_CheckedChanged(object sender, EventArgs e)
        {

        }
	}
}
